// import Vue from 'vue'
import {
  SET_TOKEN,
  DESTROY_TOKEN,
  SET_USERS,
  SHOW_SUCCESS_MSG,
  SHOW_ERROR_MSG,
} from '../mutation-types'
import {
  getAccessToken
} from '@/api/user';

// initial state
const state = {
  currentuser: {
    _id: window.localStorage.currentuser_id,
    username: window.localStorage.currentuser_username,
    account: window.localStorage.currentuser_account,
    roleCode: window.localStorage.currentuser_roleCode,
    permissions: JSON.parse(window.localStorage.currentuser_permissions || '[]')
  }
}


// getters
const getters = {
  submitVoteButtonVisibility: state => {
    let visibility = false;
    state.currentuser.permissions.map(permission => {
      if (permission.permissionType === "POST" && permission.permissionUrl === "/api/vote") {
        visibility = true;
      }
    })
    return visibility;
  },
  deleteVoteButtonDisabled: state => {
    let disabled = true;
    state.currentuser.permissions.map(permission => {
      if (permission.permissionType === "DELETE" && permission.permissionUrl === "/api/vote") {
        disabled = false;
      }
    })
    return disabled;
  }
}

// actions
const actions = {
  reqGetAccessToken({
    commit,
    state
  }, payload) {
    //登录前销毁本地令牌信息
    commit(DESTROY_TOKEN);
    return new Promise((resolve, reject) => {
      getAccessToken(payload).then(response => {
        let {
          data: result
        } = response;
        if (result.states == 200) {
          commit(SET_TOKEN, result.results);
          let {
            _id,
            username,
            account,
            roles: [{
              roleCode,
              permissions
            }]
          } = result.results.payload;
          commit(SET_USERS, {
            _id,
            username,
            account,
            roleCode,
            permissions
          });
          commit(SHOW_SUCCESS_MSG, {
            message: '登录成功',
            needRedirect: true,
            redirectPathName: 'Main',
            messagecode: `get-access-token-${result.states}`
          });
        } else {
          commit(SHOW_ERROR_MSG, {
            message: result.err,
            messagecode: `get-access-token-${result.states}`
          });
        }
        resolve(response);
      }).catch(error => {
        reject(error)
      });
    })
  },
  //注销登录
  logout({
    commit,
    state
  }, payload) {
    //销毁本地令牌信息
    commit(DESTROY_TOKEN);
    commit(SHOW_SUCCESS_MSG, {
      time: 1000,
      needRedirect: true,
      redirectPathName: 'Login',
      messagecode: `logout-success`
    });
  }
}

// mutations
const mutations = {
  //保存用户信息
  [SET_USERS](state, user) {
    //  debugger;
    window.localStorage.currentuser_id = user._id;
    window.localStorage.currentuser_username = user.username;
    window.localStorage.currentuser_account = user.account;
    window.localStorage.currentuser_roleCode = user.roleCode;
    window.localStorage.currentuser_permissions = JSON.stringify(user.permissions);

    state.currentuser = { ...state.currentuser,
      ...user
    };

    // Vue.set(state, '_id', user._id);
    // Vue.set(state, 'username', user.username);
    // Vue.set(state, 'account', user.account);
    // Vue.set(state, 'roleCode', user.roleCode);
    // Vue.set(state, 'permissions', user.permissions);
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
